import { ipcRenderer } from "electron";
import fs from 'fs';
import { exec } from 'child_process';


export const getConfig = async (): Promise<any> => {
    let rezult = await ipcRenderer.invoke("getConfig");
    return rezult;
}

/**
 * 向操作系统发送通知
 * @param title 
 * @param body 
 */
export const sendNotification = (title: string, body: string) => {
    ipcRenderer.send("sendNotification", title, body);
}

/**
 * 获取CPU信息
 * @returns 
 */
export const getCPUInfo = async (): Promise<{ arch: string }> => {
    return new Promise<{ arch: string }>(async (resolve, reject) => {
        exec('uname -m', (err, stdout, stderr) => {
            if (err) {
                reject(err);
                return;
            }
            resolve({ arch: stdout.trim() });
        });
    });
}

/**
 * 获取系统信息
 * @returns 
 */
export const getOSInfo = async (): Promise<{ os: string; version: string; }> => {
    return new Promise<{ os: string; version: string }>((resolve, reject) => {
        fs.readFile('/etc/os-release', 'utf8', (err, data) => {
            if (err) {
                reject(err);
                return;
            }

            const os = data.match(/NAME="([^"]+)"/)?.[1];
            const version = data.match(/VERSION="([^"]+)"/)?.[1];

            if (os && version) {
                resolve({ os, version });
            }
            else if (os) {
                resolve({ os, version: '未知' });
            }
            else if (version) {
                resolve({ os: '未知', version });
            }
            else {
                reject(new Error('获取操作系统信息失败'));
            }
        });
    });
}

/**
 * 获取系统主题
 * @returns 
 */
export const getOSTheme = async (): Promise<string> => {
    let rezult = await ipcRenderer.invoke("getDarkMode");
    return rezult;
}